/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
PlayerStateMachine = function(doc_player)
{
    this.playList = new Array();
    this.nowPlaying = null;
    this.playbackSpeed = 0;

    // setup the player and register for a few callbacks
    this.player = doc_player;
    this.player.OnRenderingComplete = function() {
        this.complete();
    };
    this.player.OnNetworkDisconnection = function() {
        this.stop();
    };
};


PlayerStateMachine.prototype = {
    onStartup: function(event, from, to)
    {
        //this.player.Open();
    },
    onaddToPlayList: function(event, from, to, url)
    {
        this.playList.push(url);
    },
    onstop: function(event, from, to)
    {
        this.player.Stop();
    },
    onpause: function(event, from, to)
    {
        this.player.Pause();
    },
    onbeforeplay: function(event, from, to)
    {
        // cancel transition to playing if nothing to play
        return this.playList.length > 0;
    },
    onplaying: function(event, from, to)
    {
        index = jQuery.inArray(this.nowPlaying, this.playList);
        if (index > -1 && index < this.playList.length)
        {
            this.nowPlaying = this.playList[index];
        }
        else
        {
            this.nowPlaying = this.playList[0];
        }
        this.playbackSpeed = 0;
        this.player.Play(this.nowPlaying);
        //Audio.plugin.SetSystemMute(false);
    },
    onskipForward: function(event, from, to)
    {
        index = jQuery.inArray(this.nowPlaying, this.playList);
        if (index > -1)
        {
            index = index + 1;
        }
        if (index > -1 && index < this.playList.length)
        {
            this.nowPlaying = this.playList[index];
        }
        else
        {
            this.nowPlaying = this.playList[0];
        }
        this.player.Play(this.nowPlaying);
    },
    onskipBackward: function(event, from, to)
    {
        index = jQuery.inArray(this.nowPlaying, this.playList);
        if (index > -1)
        {
            index = index - 1;
        }
        if (index < 0)
        {
            index = this.playList.length - 1;
        }
        this.nowPlaying = this.playList[index];
        this.player.Play(this.nowPlaying);
    },
    onspeedUp: function(event, from, to)
    {
        this.playbackSpeed += 2;
        if (this.playbackSpeed === 0)
        {
            this.playbackSpeed = 2;
        }
        this.player.SetPlaybackSpeed(this.playbackSpeed);
    },
    onspeedDown: function(event, from, to)
    {
        this.playbackSpeed -= 2;
        if (this.playbackSpeed === 0)
        {
            this.playbackSpeed = -2;
        }
        this.player.SetPlaybackSpeed(this.playbackSpeed);
    },
    onbeforecomplete: function(event, from, to)
    {
        index = jQuery.inArray(this.nowPlaying, this.playList);
        if (from === to)
        {
            // we have more to play? then stay in playing
            return index < (this.playList.length - 1);
        }
        else
        {
            // we have no more to play, so move to waiting
            return index === (this.playList.length - 1);
        }
    },
    oncomplete: function(event, from, to)
    {
        if (from === to)
        {
            this.onskipForward();
        }
        else
        {
            this.nowPlaying = null;
            this.playList.splice(0, this.playList.length);
        }

    },
    onclearPlayList: function(event, from, to)
    {
        this.nowPlaying = null;
        this.playList.splice(0, this.playList.length);
    }


};

StateMachine.create({
    target: PlayerStateMachine.prototype,
    events: [
        {name: "startup", from: "none", to: "waiting"},
        {name: "addToPlayList", from: "waiting", to: "waiting"},
        {name: "play", from: "waiting", to: "playing", args: "url"},
        {name: "complete", from: "playing", to: ["playing", "waiting"]},
        {name: "addToPlayList", from: "playing", to: "playing"},
        {name: "stop", from: "playing", to: "waiting"},
        {name: "pause", from: "playing", to: "paused"},
        {name: "addToPlayList", from: "paused", to: "paused"},
        {name: "play", from: "paused", to: "playing"},
        {name: "skipForward", from: "playing", to: "playing"},
        {name: "skipBackward", from: "playing", to: "playing"},
        {name: "speedUp", from: "playing", to: "seeking"},
        {name: "speedDown", from: "playing", to: "seeking"},
        {name: "speedUp", from: "seeking", to: "seeking"},
        {name: "speedDown", from: "seeking", to: "seeking"},
        {name: "stop", from: "seeking", to: "waiting"},
        {name: "play", from: "seeking", to: "playing"},
        {name: "clearPlayList", from: "waiting", to: "waiting"}]
});
